package com.haynes.meetxiaohai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haynes.meetxiaohai.common.CustomException;
import com.haynes.meetxiaohai.dto.DishDto;
import com.haynes.meetxiaohai.entity.Dish;
import com.haynes.meetxiaohai.entity.DishFlavor;
import com.haynes.meetxiaohai.mapper.DishMapper;
import com.haynes.meetxiaohai.service.DishFlavorService;
import com.haynes.meetxiaohai.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Haynes
 * @Date 2023/3/20 11:19
 * @PackageName:com.haynes.reggie.service.impl
 * @ClassName: DishServiceImpl
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService flavorService;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dto) {
//        1.保存菜品基本信息
        dto.setCode("1001");
        this.save(dto);
//        循环获取菜品口味信息，然后设置到对应的菜品
        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor item : flavors) {
            item.setDishId(dto.getId());
        }
//        2.保存菜品口味信息
        flavorService.saveBatch(flavors);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
//        1.首先查询菜品信息
        Dish dish = this.getById(id);
//      复制菜品基本信息到VO对象中
        DishDto dto = new DishDto();
        BeanUtils.copyProperties(dish,dto);
//        根据菜品id查对应的口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = flavorService.list(dishFlavorLambdaQueryWrapper);

        dto.setFlavors(list);
        return dto;
    }

    @Override
    @Transactional
    public void updateByIdWithFlavor(DishDto dto) {
//        1.先保存当前菜品的基本信息
        this.updateById(dto);
//        2.之后删除当前ID口味信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dto.getId());
        flavorService.remove(queryWrapper);
//        3.之后再新增口味信息
        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor item : flavors) {
//            设置口味信息表中的菜品ID
            item.setDishId(dto.getId());
        }
        flavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public boolean removeByIdBatch(List<Long> ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        实例： in("id", Arrays.asList(1, 2, 3, 4, 5))
        dishLambdaQueryWrapper.in(ids!=null,Dish::getId,ids);

        List<Dish> dishList = this.list(dishLambdaQueryWrapper);
        for (Dish item : dishList) {
            if (item.getStatus()==0){
                this.removeByIds(ids);
                flavorService.removeByIds(ids);
                return true;
            }else {
                throw new CustomException("当前菜品正在售卖中无法删除!");
            }
        }
        return false;
    }

    @Override
    public boolean updateStatusByIdBatch(Integer status, List<Long> ids) {
        if (ids!=null && status !=null){
            List<Dish> dishlistByIds = this.listByIds(ids);
            for (Dish item : dishlistByIds) {
                item.setStatus(status);
                this.updateById(item);
                return true;
            }
        }
        return false;
    }


}
